﻿using UnityEngine;
using System.Collections;

public class DialoguerPrinter : MonoBehaviour {
	
	[HideInInspector] public string _dialogue;
	[HideInInspector] public string _name;
	[HideInInspector] public GameObject mouseDetector;
	[HideInInspector] public GameObject hotspots;
	[HideInInspector] public GameObject gameDialogue; //<-- Text Label
	[HideInInspector] public UILabel charName;
	[HideInInspector] public bool isSkipped;
	[HideInInspector] public bool isAuto;
	[HideInInspector] public float autoSpeed;
	[HideInInspector] public GameObject dialogueManager;
	[HideInInspector] public bool isCutscene;
	[HideInInspector] public AC.Cutscene nextCutscene;

	void Awake(){
		Dialoguer.Initialize ();
		Dialoguer.events.onStarted += onStart;
		Dialoguer.events.onEnded += onEnded;
		Dialoguer.events.onTextPhase += onTextPhase;
		gameDialogue = GameObject.Find("text");
	}

	// Use this for initialization
	void Start () {
		hotspots = GameObject.Find ("_Hotspots");
		mouseDetector = GameObject.Find ("DialoguerMouseDetector");
		charName = GameObject.Find("nametext").GetComponent<UILabel>();
		//isShowing = true;
		//Dialoguer.StartDialogue(Dialogue_ID, DialoguerCallback);
		//GameObject.FindWithTag (Tags.gameEngine).GetComponent<KickStarter> ().TurnOffAC ();
	}
	
	// Update is called once per frame
	void Update () {

		if(!gameDialogue.activeSelf)
			gameDialogue.SetActive(true);
		charName.text = _name;
		if (Input.GetKeyDown (KeyCode.Space))
		{
			if(gameDialogue.GetComponent<TypewriterEffect>() != null && isAuto == true)
			{
				Destroy(gameDialogue.GetComponent<TypewriterEffect>());
			}
			Dialoguer.ContinueDialogue();
		}
		if(isAuto == true)
			if(gameDialogue.GetComponent<TypewriterEffect>() == null)
				StartCoroutine(autoDialogue());
	}

	void FixedUpdate()
	{
		if(isSkipped == true)
		{
			Dialoguer.ContinueDialogue();
			if(gameDialogue.GetComponent<TypewriterEffect>() != null)
				Destroy (gameDialogue.GetComponent<TypewriterEffect>());
			gameDialogue.SetActive(false);
		}
	}

	IEnumerator autoDialogue()
	{
		isAuto = false;
		yield return new WaitForSeconds (1);
		Dialoguer.ContinueDialogue ();
		isAuto = true;
	}

	void OnGUI()
	{
		if (GUI.Button (new Rect (Screen.width - 250, Screen.height - 50, 120, 20), "Auto")) {
			print ("auto complete.");
			GUI.Box (new Rect(0,0,150,30),"Auto Mode");
			isAuto = true;
		}

		if (GUI.Button (new Rect (Screen.width - 450, Screen.height - 50, 120, 20), "Skip")) {
			print ("skip text.");
			GUI.Box (new Rect(0,0,150,30),"Skip Mode");
			isSkipped = true;
			//Destroy(gameDialogue.GetComponent<TypewriterEffect>());
		}

		/*if (isShowing)
		{		
			GUI.Box(new Rect(0, Screen.height - 235, 200, 30),_name);
			GUI.Box(new Rect(0, Screen.height - 200, Screen.width ,150),_dialogue);
		}
		else
			return;*/
	}
	
	private void onStart()
	{
		Debug.Log("OnStart ran!");
		if(isAuto)
			StartCoroutine(autoDialogue());
	}

	public void ChangeCutscene(AC.Cutscene cutsceneName)
	{
		GameObject.Find (cutsceneName.name).GetComponent<AC.ActionList> ().BeginActionList (0);
		GameObject.Find (cutsceneName.name).GetComponent<AC.ActionList> ().Reset ();
	}
	
	private void onEnded()
	{
		Debug.Log ("isCutscene is " + isCutscene);
		if(isCutscene)
		{
			GameObject.FindWithTag (Tags.gameEngine).GetComponent <KickStarter>().TurnOnAC ();
			ChangeCutscene(nextCutscene);
		}
		else
		{
			GameObject.FindWithTag (Tags.gameEngine).GetComponent <KickStarter>().TurnOnAC ();
			//deactivate mouse detector
			mouseDetector.SetActive (false);
			//Return hotspots to original default layer
			SetLayerRecursively (hotspots, 0);
		}

	}
	
	private void onTextPhase(DialoguerTextData data)
	{
		_dialogue = data.text;
		_name = data.name;

		//GameObject.FindWithTag (Tags.persistentEngine).GetComponent<KickStarter> ().TurnOffAC ();

		gameDialogue.GetComponent<UILabel>().text = _dialogue;
		gameDialogue.SetActive(false);
		gameDialogue.AddComponent("TypewriterEffect");
	}
	
	private void DialoguerCallback()
	{
		this.enabled = true;
	}
	
	private void SetLayerRecursively(GameObject obj, int newLayer)
	{
		if (null == obj)
		{
			return;
		}
		
		obj.layer = newLayer;
		
		foreach (Transform child in obj.transform)
		{
			if (null == child)
			{
				continue;
			}
			SetLayerRecursively(child.gameObject, newLayer);
		}
	}
	
	
}
